/*
 * Date: 12/3/2011
 * 
 * Author: Jason Herrboldt (herrb005@umn.edu)
 * 
 * Project: CSCI 5115 AutoAlarm Android App
 *          List View Activity  
 *  
 * Notes:
 * 
 * Creates its own list of fake AAEvent objects and
 * groups the events by date to be sent to the list
 * adapter.  To see fake data in list view, change 
 * dates of spoofed AAEvents below -- see getAAEventList().  
 * 
 * TODO: Still need to import list of real AAEvent objects. 
 * 
 * TODO: Still need to add touch event to take user to
 * event page.
 * 
 */

package csci5115.autoalarm;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;

public class ListViewActivity extends ListActivity {
	
	public ArrayList<AAEvent> eventlist;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_list_view);
       
        updateList();
       
//        
//        SimpleAdapter adapter = new SimpleAdapter(this,
//		listData, R.layout.custom_row_view,
//    		new String[] {"eventName", "eventTime"},
//         	new int[] {R.id.text1, R.id.text2});
//        getAAEventList();
//        populateListData();
//        setListAdapter(adapter);
    }
    
    public void onResume() {
    	super.onResume();
    	updateList();
    }
    
    /*
     *     VARIABLES AND DATA STRUCTURES
     */
	private Date todaysDate = new Date();
	private int day = todaysDate.getDate();
	private int month = todaysDate.getMonth() + 1;
	private int year = todaysDate.getYear() + 1900;
	String dayString = Integer.toString(day);
	String monthString = Integer.toString(month);
	String yearString = Integer.toString(year);
    
	// Data structure for adapter
    ArrayList<HashMap<String,String>> listData = 
        	new ArrayList<HashMap<String,String>>();
    
    // List of AA calendar events
    ArrayList<AAEvent> listOfAAEvents = new ArrayList<AAEvent>();
    
    private void getAAEventList() {
    	
    	/*
    	 * TODO: Get real AAEvent list
    	 */
    	
    	// Populate list with fake data for testing. 	
    	// NOTE: To see output with fake data, change dates below
    	// to correspond to current date. 
	    String eventTitle1 = "Breakfast At Tiffany's";
	    Date eventDate1 = new Date();
	    eventDate1.setDate(4); // Make this on or near today's date
	    eventDate1.setMonth(11); // Make this the current month
	    eventDate1.setYear(2011 - 1900); // Make this the current year
	    eventDate1.setHours(8);
	    eventDate1.setMinutes(30);
	    eventDate1.setSeconds(0);
	    Boolean alarmStatus1 = true;
	    String alarmType1 = "Alarm Type";
	    byte transportation1 = 2;
	    String location1 = new String("hi");
	    AAEvent event1 = new AAEvent(eventTitle1, eventDate1, alarmStatus1, alarmType1, transportation1, location1);
	    listOfAAEvents.add(event1);
	    
	    String eventTitle2 = "Dinner With Andre";
	    Date eventDate2 = new Date();
	    eventDate2.setDate(5);
	    eventDate2.setMonth(11);
	    eventDate2.setYear(2011 - 1900);
	    eventDate2.setHours(23);
	    eventDate2.setMinutes(30);
	    eventDate2.setSeconds(0);
	    Boolean alarmStatus2 = true;
	    String alarmType2 = "Alarm Type";
	    byte transportation2 = 2;
	    String location2 = new String("hi");
	    AAEvent event2 = new AAEvent(eventTitle2, eventDate2, alarmStatus2, alarmType2, transportation2, location2);
	    listOfAAEvents.add(event2);
	    
	    String eventTitle3 = "Coffee With Uma";
	    Date eventDate3 = new Date();
	    eventDate3.setDate(6);
	    eventDate3.setMonth(11);
	    eventDate3.setYear(2011 - 1900);
	    eventDate3.setHours(13);
	    eventDate3.setMinutes(15);
	    eventDate3.setSeconds(0);
	    Boolean alarmStatus3 = false;
	    String alarmType3 = "Alarm Type";
	    byte transportation3 = 1;
	    String location3 = new String("hi");
	    AAEvent event3 = new AAEvent(eventTitle3, eventDate3, alarmStatus3, alarmType3, transportation3, location3);
	    listOfAAEvents.add(event3);
	    
	    String eventTitle4 = "AI Colloquium";
	    Date eventDate4 = new Date();
	    eventDate4.setDate(6);
	    eventDate4.setMonth(11);
	    eventDate4.setYear(2011 - 1900);
	    eventDate4.setHours(18);
	    eventDate4.setMinutes(00);
	    eventDate4.setSeconds(0);
	    Boolean alarmStatus4 = true;
	    String alarmType4 = "Alarm Type";
	    byte transportation4 = 3;
	    String location4 = new String("hi");
	    AAEvent event4 = new AAEvent(eventTitle4, eventDate4, alarmStatus4, alarmType4, transportation4, location4);
	    listOfAAEvents.add(event4);
	    
    }
    
    public void updateList() {
		// access event database
		EventDBAdapter db = new EventDBAdapter(this);
		db.open();
		Cursor data = db.getAllEvents();

		// Array to store events
		eventlist = new ArrayList<AAEvent>();
 
		if (data.getCount() > 0) {
			startManagingCursor(data);
			data.moveToFirst();
			// Add each event to event list
			while (!data.isAfterLast()) {
				AAEvent e = new AAEvent(data);
				eventlist.add(e);
				data.moveToNext();
			}
			
		}
		db.close();
		data.close();
		EventViewAdapter ev = new EventViewAdapter(this, eventlist);
		setListAdapter(ev);
	}
    
    /*
     * Old logic for reference
     */
    
    // Populate data structure for export to adapter
    private void populateListData() {
    	Iterator<AAEvent> aaEventIterator = listOfAAEvents.iterator();
    	String todaysDateString = makeStringDate(todaysDate);
    	String currentGroupDate = "0";
    	while(aaEventIterator.hasNext()) {
    		HashMap<String,String> temp = new HashMap<String,String>();
    		AAEvent currentEvent = aaEventIterator.next();
    		String currentEventDateString = makeStringDate(currentEvent.mDate);
    		if(todaysDate.compareTo(currentEvent.mDate) <= 0) {
    			if(currentEventDateString.equals(currentGroupDate)) {
    				insertData(temp, currentEvent);
    			} else {
    				insertHeaderRow(todaysDateString, currentEventDateString);
					currentGroupDate = currentEventDateString;
					insertData(temp, currentEvent);
    			}
    		}
    	}
    }
    
    private void insertData(HashMap<String, String> temp, AAEvent currentEvent) {
		temp.put("eventName", currentEvent.mTitle);
		if(currentEvent.mAlarmStatus) {
			// temp.put("AlarmStatus", "Alarm ON");
			temp.put("eventTime", converToCivilianTime(currentEvent.mDate) + " - Alarm ON");
		} else {
			temp.put("eventTime", converToCivilianTime(currentEvent.mDate) + " - Alarm OFF");
		}
		listData.add(temp);
    }
    
    // Convert time, e.g. "18:45:00" to "6:45 PM"
    private String converToCivilianTime(Date date) {
    	int hour = date.getHours();
    	int minute = date.getMinutes();
    	String hourString = "X";
    	String minuteString = "X";
    	String AMorPM;
    	if(hour > 12) {
    		AMorPM = "PM";
    		hourString = Integer.toString(hour = hour % 12);
    	} else {
    		AMorPM = "AM";
    		hourString = Integer.toString(hour);
    	}
    	if(minute < 10) {
    		if(minute == 0) {
    			minuteString = "00";
    		} else {
    			minuteString = "0" + Integer.toString(minute);
    		}
    	} else {
    		minuteString = Integer.toString(minute);
    	}
    	return hourString + ":" + minuteString + " " + AMorPM;
    }
    
    // Convert date variable to string, e.g. "YYYYMMDD"
    private String makeStringDate(Date eventDate) {
    	int month = eventDate.getMonth();
    	int date = eventDate.getDate();
    	int year = eventDate.getYear();
    	
    	String monthString = ""; 
    	String dateString = "";
    	String yearString = "";    	
    	
    	if(month < 10) {
    		monthString = "0" + Integer.toString(month);
    	} else {
    		monthString = Integer.toString(month);
    	}
    	
    	if(date < 10) {
    		dateString = "0" + Integer.toString(date);
    	} else {
    		dateString = Integer.toString(date);
    	}
    	
    	yearString = Integer.toString(year + 1900);
    	return yearString + monthString + dateString;
    }
    
    // Insert a row that either reads, "Today" or "MM/DD/YYYY"
    private void insertHeaderRow(String today, String eventDate) {
		HashMap<String,String> temp = new HashMap<String,String>();
		if(today.equals(eventDate)) {
	    	temp.put("eventName","    Today");
			temp.put("eventTime", "");
		}
		else {
	    	temp.put("eventName", formatDate(eventDate));
	    	temp.put("eventTime", "");
		}
		listData.add(temp);
    }
    
    // Convert "YYYYMMDD" to "MM/DD/YYYY"
	private String formatDate(String dateToFormat) {
		dateToFormat = "    " + dateToFormat.substring(4, 6) + "/" + 
				dateToFormat.substring(6, dateToFormat.length()) + "/" + 
				dateToFormat.substring(0, 4);
		return dateToFormat;
	}   
}